<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid']);	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	if(isset($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$total='';
	$fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		//创建时间
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);	
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		foreach($list as $k=>&$v){
			$params['goodsid']=$v['id'];
			$v['saletotal']=pdo_getsum('jy_weishop_order_goods',$params,'total');
			$v['saletotal_price']=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：下单时间为准！','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'saletotal_price','title'=>'销售额(商品价/不减优惠价/不算运费)','type'=>'string'),
			array('name'=>'saletotal','title'=>'销量','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>13,'title'=>'选择商品','name'=>'id','action'=>'jy_weishop/goods.get_goods/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);	
	if(isset($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_weishop_goods',$params);
	paixu($list,'saletotal','desc');
	if($list){
		//创建时间
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);	
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		foreach($list as $k=>&$v){
			$params['goodsid']=$v['id'];
			$v['saletotal']=pdo_getsum('jy_weishop_order_goods',$params,'total');
			$v['saletotal_price']=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
	}
	$header=array(
		'title'=>'商品名称',
		'saletotal_price'=>'销售额(商品价/不减优惠价/不算运费)',
		'saletotal'=>'销量',
	);
	cfc('tools')->export2excel($header,$list,'商品统计');
}
